import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/user.dart';
import 'package:qgchat/page/live/dynamic/BloC/dynamicBloC.dart';
import 'package:qgchat/states/states_model/index.dart';

class DynamicFollow extends StatelessWidget {
  const DynamicFollow(this.isFollow, this.uid, {Key key}) : super(key: key);

  final bool isFollow; // 当前是否关注
  final int uid; // 主播id

  @override
  Widget build(BuildContext context) {
    UserState user = state(context, 'user');
    if (user.info.memberType == 1) {
      return Container();
    }

    return Container(
      width: px(120),
      height: px(50),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(px(25)),
        child: Builder(
          builder: (BuildContext context) {
            if (isFollow) {
              return FlatButton(
                onPressed: () async {
                  Map res = await UserApis.cancelfollow(uid);
                  if (res['status'] == 1) {
                    showToast("取消关注成功");
                    BlocProvider.of<DynamicBloc>(context).add(DynamicEvent.refresh);
                  }
                },
                padding: EdgeInsets.zero,
                color: col("e6e6e6"),
                child: Text(
                  "不关注",
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: px(26),
                  ),
                ),
              );
            } else {
              return FlatButton(
                onPressed: () async {
                  Map res = await UserApis.follow(uid);
                  if (res['status'] == 1) {
                    showToast("关注成功");
                    BlocProvider.of<DynamicBloc>(context).add(DynamicEvent.refresh);
                  }
                },
                padding: EdgeInsets.zero,
                color: col("ffb240"),
                child: Text(
                  "关注",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: px(26),
                  ),
                ),
              );
            }
          }
        ),
      ),
    );
  }
}
